<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '商品分类添加' : '商品分类编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
    {{user}}
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <el-select
            v-model="user.pid"
            placeholder="请选择"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <div class="upload">
            <p>+</p>
            <input type="file" @change="changeImg" v-if="info.isshow">
            <img :src="imgUrl" alt="" v-if="imgUrl">
          </div>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="user.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd">添 加</el-button>
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { cateadd, cateedit, cateinfo } from '../../../http/api';

import { errorAlert, successAlert } from "../../../utils/alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      user: {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      imgUrl:""
    };
  },
  computed: {
    ...mapGetters({
      list:"cate/list"
    }),
  },
  methods: {
    ...mapActions({
      reqList:"cate/reqList"
    }),
    //添加
    add(){
      console.log(this.user);
      cateadd(this.user).then(res=>{
        if(res.data.code==200){
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.reqList()
        }
      })
    },
    changeImg(e){
      let file=e.target.files[0];

      //判断文件类型
      let imgArr = [".jpg",".jpeg",".png",".gif"];
      //原生js获取后缀名
      // let extname=file.name.slice(file.name.lastIndexOf("."))
      let extname = path.extname(file.name);
      if(!imgArr.includes(extname)){
        errorAlert("请上传正确的文件格式");
        return;
      }

      //根据文件生成一个图片地址,仅显示给用户看
      this.imgUrl = URL.createObjectURL(file);
      //赋值给user
      this.user.img=file;
    },
    //取消
    cancel(){
      this.info.isshow=false
    },
    //清空
    empty(){
      this.user={
        pid: "",
        catename: "",
        img: "",
        status: 1,
      }
      this.imgUrl="";
    },

    
    //获取一个数据
    getOne(id){
      cateinfo({id}).then(res=>{
        if(res.data.code==200){
          this.user=res.data.list;
          this.imgUrl=this.$pre+res.data.list.img;
          //补id字段,发送修改时需要使用
          this.user.id=id
        }
      })
    },
    update(){
      cateedit(this.user).then(res=>{
        if(res.data.code=200){
          successAlert(res.data.msg);
          this.info.isshow=false;
          this.empty();
          //通知父组件刷新表格
          this.reqList();
        }
      })
    },
    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed(){
      if(!this.info.isAdd){
        this.empty()
      }
    }
  },
};
</script>

<style scoped>
.upload{
  position: relative;
  width: 150px;
  height: 150px;
  border: 1px dashed #333;
}
.upload p{
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 150px;
}
.upload input{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 100;
  cursor: pointer;
}
.upload img{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>